<?php

include '../inc/includes.php';

if (!isset($_GET['show'])) {
	header('HTTP/1.0 400 Bad Request');
	die('{"status": "error","code": 400,"message" : "Missing or incorrect syntax"}');
}

$show = $_GET['show'];
$shows = array('raw', 'smackdown', 'nxt', 'wwealumni', 'halloffame');
$roster = array();

if (!in_array($show, $shows)) {
	header('HTTP/1.0 404 Not Found');
	die('{"status": "error","code": 404,"message" : "Entity Not Found"}');
}

$html = scrape('http://www.wwe.com/superstars');

// extract all superstars then delimit by show
$regex = sprintf('/<div class="(star(?: champion-(?:heavy|tag|diva|wwe|inter|us))? %s .*?)"[^>]+>(.*?)<\/div>/s', $show);
preg_match_all($regex, $html, $r);

// extract superstar name, link, image and title if it exists
foreach($r[1] as $key => $value) {
	preg_match_all('/">(.*?)<\/a.*?href="(.*?)"><img .*? data-fullsrc="(.*?)"/s', $r[2][$key], $s);
	$superstar = new stdClass();
	$superstar->name = strip_tags($s[1][0]);
	$superstar->url = 'http://www.wwe.com'. $s[2][0];
	$superstar->image = 'http://www.wwe.com'. $s[3][0];
	$superstar->class = $value;
	$superstar->slug = basename($superstar->url, '/');

	$roster[] = $superstar;
}

die(_json_encode($roster));